package com.course.masterservice.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.course.component.http.BasePageResponse;
import com.course.component.util.CopyUtil;
import com.course.component.util.SnowFlake;
import com.course.dto.reqs.RequestUser;
import com.course.dto.resp.ResponsePermissiom;
import com.course.entity.Permission;
import com.course.entity.Role;
import com.course.entity.User;
import com.course.masterservice.UserMasterService;
import com.course.service.IRoleService;
import com.course.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Copyright: Copyright (c)  林中小鸟  All rights reserved.
 * @author: moon
 * @date: 2022/9/20 13:50
 * @Description: **********************
 */
@Service
public class UserMasterServiceImpl implements UserMasterService {
    @Autowired
    private UserService userService;
    @Autowired
    private IRoleService roleService;

    @Override
    public BasePageResponse<User> list(RequestUser requestUser) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        IPage<User> list = userService.page(new Page<>(requestUser.getPage().intValue(),requestUser.getPageSize().intValue()),queryWrapper);
        List<User> records = list.getRecords();
        records = records.stream().map(item->{
            item.setRoles(userService.getUserRolAsSet(item));
            return item;
        }).collect(Collectors.toList());

        return new BasePageResponse(list.getTotal(),records);
    }

    @Transactional
    @Override
    public Boolean del(RequestUser requestUser) {
        return userService.removeById(requestUser.getId()) && userService.unLinkRoles(requestUser.getId());
    }

    @Transactional
    @Override
    public Boolean update(RequestUser requestUser) {
        User updateData = CopyUtil.copy(requestUser,User.class);
        if(ObjectUtils.isEmpty(updateData.getId())){
            updateData.setId(new SnowFlake().nextId());
            updateData.setPassword(SecureUtil.md5(updateData.getPassword()));
        }else{
            User sql = userService.getById(updateData.getId());
            if(!sql.getPassword().equals(updateData.getPassword())){
                updateData.setPassword(SecureUtil.md5(updateData.getPassword()));
            }
        }
        updateData.setPassword(SecureUtil.md5(updateData.getPassword()));
        List<Long> collect = requestUser.getRoles().stream().map(item->{
            QueryWrapper<Role> roleQueryWrapper = new QueryWrapper<>();
            roleQueryWrapper.eq("name",item);
            Role one = roleService.getOne(roleQueryWrapper);
            return one.getId();
        }).collect(Collectors.toList());
        return userService.saveOrUpdate(updateData) && userService.linkRoles(updateData.getId(),collect);

    }
}
